package com.google.code.sms.controller;

import com.google.code.sms.domain.Player;
import com.google.code.sms.domain.TransferStatus;
import com.google.code.sms.service.StatusService;
import com.google.code.sms.util.FileUtil;
import com.google.code.sms.util.StringUtil;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Controller for the status page.
 *
 * @author Sindre Mehus
 */
public class StatusController extends ParameterizableViewController {

    private StatusService statusService;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();

        List<TransferStatus> streamStatuses = statusService.getAllStreamStatuses();
        List<TransferStatus> downloadStatuses = statusService.getAllDownloadStatuses();

        Locale locale = RequestContextUtils.getLocale(request);
        List<TransferStatusHolder> transferStatuses = new ArrayList<TransferStatusHolder>();

        for (int i = 0; i < streamStatuses.size(); i++) {
            if(!streamStatuses.get(i).terminated()) {
                transferStatuses.add(new TransferStatusHolder(streamStatuses.get(i), true, false, i, locale));
            }
        }
        for (int i = 0; i < downloadStatuses.size(); i++) {
            if(!downloadStatuses.get(i).terminated()) {
                transferStatuses.add(new TransferStatusHolder(downloadStatuses.get(i), false, true, i, locale));
            }
        }

        String category = request.getParameter("category");
        if (category == null) {
            category = "current";
        }
        
        map.put("transferStatuses", transferStatuses);
        map.put("chartWidth", StatusChartController.IMAGE_WIDTH);
        map.put("chartHeight", StatusChartController.IMAGE_HEIGHT);
        map.put("category", category);

        ModelAndView result = super.handleRequestInternal(request, response);
        result.addObject("model", map);
        return result;
    }

    public void setStatusService(StatusService statusService) {
        this.statusService = statusService;
    }

    public static class TransferStatusHolder {

        private TransferStatus transferStatus;
        private boolean isStream;
        private boolean isDownload;
        private int index;
        private Locale locale;

        public TransferStatusHolder(TransferStatus transferStatus, boolean isStream, boolean isDownload,
                int index, Locale locale) {
            this.transferStatus = transferStatus;
            this.isStream = isStream;
            this.isDownload = isDownload;
            this.index = index;
            this.locale = locale;
        }

        public boolean isStream() {
            return isStream;
        }

        public boolean isDownload() {
            return isDownload;
        }

        public int getIndex() {
            return index;
        }

        public Player getPlayer() {
            return transferStatus.getPlayer();
        }

        public String getPlayerType() {
            Player player = transferStatus.getPlayer();
            return player == null ? null : player.getType();
        }

        public String getUsername() {
            Player player = transferStatus.getPlayer();
            return player == null ? null : player.getUsername();
        }

        public String getPath() {
            return FileUtil.getShortPath(transferStatus.getFile());
        }

        public String getBytes() {
            return StringUtil.formatBytes(transferStatus.getBytesTransfered(), locale);
        }
    }
}
